<template>
    <transition name="modal">
        <div class="modal-mask">
            <div class="modal-wrapper">
                <div class="modal-container">
    
                    <div class="modal-header">
                        <div class="row">
                            <div class="col-lg-3">
                                <h3>查看结果</h3>
                            </div>
                            <div class="col-lg-9 text-right">
                                <span><b>分析人员：</b>{{sample.test_user}}</span>&nbsp;&nbsp;&nbsp;
                                <span><b>检出数：</b>{{sample.manual_irregular_count=='0'?'无':sample.manual_irregular_count}}</span>&nbsp;&nbsp;&nbsp;
                                <span><b>是否广告：</b>{{sample.is_adv | isAdv}}</span>&nbsp;&nbsp;&nbsp;
                                <span class="text-danger" v-if="sample.doubt=='1'"><b>此样本有疑问</b></span>&nbsp;&nbsp;&nbsp;
                                <span><b>md5：</b><a :download="sample.sample_hash" :href="sample.download_url">{{sample.sample_hash}}</a></span>&nbsp;&nbsp;&nbsp;
                                <span><b>程序名：</b>{{sample.program_name}}</span>&nbsp;&nbsp;&nbsp;
                                <label class="checkbox-inline">
                                    <input type="checkbox" v-model="hid_standard">只显示不规范项
                                </label>&nbsp;&nbsp;&nbsp;
                            </div>
                        </div>
                        
                        
                        <button type="button" class="close" @click="$emit('close')">
                            <i class="fa fa-times" aria-hidden="true"></i>
                        </button>
                    </div>
    
                    <div class="modal-body">
                        <div class="row m-b">
                            <div class="col-lg-1"><b>序号</b></div>
                            <div class="col-lg-6"><b>检测项</b></div>
                            <div class="col-lg-1"><b>检测结果</b></div>
                            <div class="col-lg-2"><b>截图</b></div>
                            <div class="col-lg-2"><b>备注</b></div>
                        </div>
                        <div class="list-box">
                            <template v-for="(analysis,i) in res_list">
                                <div class="row m-b" :key="i" v-show="analysis.state=='1'?true:!hid_standard" v-if="analysis.id!=34">
                                    <div class="col-lg-1">{{i+1}}</div>
                                    <div class="col-lg-6">{{analysis.name}}</div>
                                    <div class="col-lg-1">
                                        <button class="btn" disabled :class="{'btn-success':analysis.state=='1','btn-default':analysis.state=='0'}">
                                            是
                                        </button>
                                    </div>
                                    <div class="col-lg-2">
                                        <img v-if="analysis.file.length>0" class="file-img" :src="getImgSrc(analysis.file[0])" @click.prevent="showImgList(analysis.file)">
                                        <span v-else>无截图</span>
                                    </div>
                                    <div class="col-lg-2">{{analysis.remark}}</div>
                                </div>
                                <div class="row m-b" :key="i" v-show="analysis.state=='1'?true:!hid_standard" v-else>
                                    <div class="col-lg-1">{{i+1}}</div>
                                    <div class="col-lg-4">{{analysis.name}}</div>
                                    <div class="col-lg-1"><div v-if="analysis.fake_info.pn"><b>仿冒对象</b>：<br>{{analysis.fake_info.pn}}</div></div>
                                    <div class="col-lg-1"><ResizeImg  v-if="analysis.fake_info.ic" :src="icon34_base_url+sample.sample_hash"></ResizeImg></div>
                                    <div class="col-lg-1">
                                        <button class="btn" disabled :class="{'btn-success':analysis.state=='1','btn-default':analysis.state=='0'}">
                                            是
                                        </button>
                                    </div>
                                    <div class="col-lg-2">
                                        <img v-if="analysis.file.length>0" class="file-img" :src="getImgSrc(analysis.file[0])" @click.prevent="showImgList(analysis.file)">
                                        <span v-else>无截图</span>
                                    </div>
                                    <div class="col-lg-2">{{analysis.remark}}</div>
                                </div>
                            </template>
                        </div>
                        <div class="row m-b m-t">
                            <div class="col-lg-8">
                                其他备注：{{sample.analyze_remark}}
                            </div>
                            <div class="col-lg-4">
                                备注截图：
                                <img v-if="analyze_remark_file.length>0" class="file-img" :src="getImgSrc(analyze_remark_file[0])" @click.prevent="showImgList(analyze_remark_file)">
                            </div>
                        </div>
                        <div class="row">
                            <div class="col-lg-8">
                                <div class="form-group">
                                    <label>审核通过/不通过原因</label>
                                    <input class="form-control" v-model="uncheck_reason">
                                </div>
                            </div>
                            <div class="col-lg-4">
                                <div class="form-group">
                                    <label>审核截图</label>
                                    <br>
                                    <AnalyzeFileInput :files="check_file" :name="'app_file'"></AnalyzeFileInput>
                                </div>
                                
                            </div>
                        </div>
                        
                    </div>
    
                    <div class="modal-footer text-center">
                        <button type="button" class="btn btn-primary" :disabled="doing" @click="check('0')">
                            <i class="fa fa-refresh fa-spin fa-fw" v-show="doing"></i>
                         审核通过 (快捷键:alt+4)</button>
                        <button type="button" class="btn btn-default" :disabled="doing" @click="check('1')">
                            <i class="fa fa-refresh fa-spin fa-fw" v-show="doing"></i>
                         审核不通过 (快捷键:alt+5)</button>
                    </div>
                </div>
            </div>
            <Gallery :files="files" :base-url="base_url" :open="img_list_show" @close="closeGallery"></Gallery>
        </div>
    </transition>
</template>
<script>
import {tip,ajax} from '../tools/tools.js'
import {cfg} from '../config/config.js'
import Gallery from './Gallery.vue'
import AnalyzeFileInput from './AnalyzeFileInput.vue'

export default {
    name: 'ApprovalSingleModal',
    components:{Gallery,AnalyzeFileInput},
    data() {
        return {
            res_list:[],
            uncheck_reason:'',

            hid_standard:false,
            base_url:cfg.baseUrl+'/Uploads/',
            img_list_show:false,
            check_file:[],
            files:[],
            analyze_remark_file:[],
            doing:false
        }
    },
    props:{
        sample:{
            type:Object,
            default:{}
        },
        opened:{
            type:Boolean,
            default:false
        }
    },
    watch:{
        opened(v){
            var self = this;
            self.bindingKey(v);
        },
        sample:{
            deep:true,
            handler(v,ov){
                var self = this;
                if(v.sample_hash){
                    self.uncheck_reason = v.uncheck_reason;
                    self.analyze_remark_file = v.file;
                    self.check_file = v.check_file;
                    self.resultView();
                } else {
                    self.uncheck_reason = '';
                    self.analyze_remark_file = [];
                    self.check_file = [];
                }
            }
        }
    },
    filters:{
        isAdv(v) {
            if (v) {
                return v == '1' ? '是' : '否'
            }
            return '';
        }
    },
    methods: {
        bindingKey(f){
            var self = this;

            if(f){
                document.body.addEventListener('keydown',self.keyEvent,false);
            } else {
                document.body.removeEventListener('keydown',self.keyEvent,false);
            }
        },
        keyEvent(e){
            var self = this;
            if(e.altKey&&e.keyCode==52){
                if (window.event) {
                    e.returnValue = false;
                    e.preventDefault();
                } else {
                    e.preventDefault();
                }
                if(self.sample.sample_hash){
                    self.check('0');
                }
            }

            if(e.altKey&&e.keyCode==53){
                if (window.event) {
                    e.returnValue = false;
                    e.preventDefault();
                } else {
                    e.preventDefault();
                }
                if(self.sample.sample_hash){
                    self.check('1');
                }
            }

            if(e.keyCode==27){
                if (window.event) {
                    e.returnValue = false;
                    e.preventDefault();
                } else {
                    e.preventDefault();
                }
                if(self.sample.sample_hash&&self.$root.GALLERY_OPENED==false){
                    self.$emit('close');
                }
            }
        },
        resultView(){
            var self = this;
            ajax.getTestResult({
                param:{
                    sample_hash:self.sample.sample_hash,
                    test_type:'3'
                },
                cb(data){
                    self.res_list = [];
                    self.res_list = data.data.map((v,i)=>{
                        if(v.id=='34'){
                            var fobj = {};
                            v.fake_info.map((f)=>{
                                if(f.fake_type=='program name'){
                                    fobj.pn = f.fake_object;
                                }
                                if(f.fake_type=='icon'){
                                    fobj.ic = f.fake_object;
                                }
                            });
                            v.fake_info = fobj;
                        }
                        return v;
                    });
                }
            })
        },
        check(type){
            var self = this;
            // self.$emit('singlecheckdone');
            self.doing = true;
            var file = [];
            [].forEach.call(document.querySelectorAll('[name=app_file]'),function(v){
                file.push(v.value);
            })
            ajax.check({
                param:{
                    sample_hash:[self.sample.sample_hash],
                    type:type,
                    uncheck_reason:self.uncheck_reason,
                    file:file
                },
                cb(data){
                    self.doing = false;
                    self.$emit('singlecheckdone');
                },
                failCb(data){
                    self.doing = false;
                }
            })
        },
        showImgList(files){
            var self = this;
            self.files = files;
            self.img_list_show = true;
        },
        closeGallery(){
            var self = this;
            self.files = [];
            self.img_list_show = false;
        },
        getImgSrc(v){
            return this.base_url + v;
        }
    },
    mounted() {
        
    }
}
</script>
<style scoped>
.list-box{
    max-height: calc( 100vh - 480px );
    overflow-y: auto;
    overflow-x: hidden;
    padding-right: 10px;
}
.list-box>.row{
    padding-top: 3px;
    border-bottom: 1px solid #eee;
}

.file-img{
    max-height: 34px;
    max-width: 34px;
    cursor: pointer;
}

.close {
    position: absolute;
    right: 30px;
    top: 30px;
}

.modal-mask {
    position: fixed;
    z-index: 10001;
    top: 0;
    left: 0;
    bottom: 0;
    right: 0;
    background-color: rgba(0, 0, 0, .5);
    transition: opacity .3s ease;
}

.modal-wrapper {
    position: absolute;
    width: 100%;
    height: 1px;
    left: 0;
    right:0;
    top: 60px;
}

.modal-container {
    width: calc( 100% - 380px );
    margin: 0px auto;
    padding: 20px 30px;
    background-color: #fff;
    border-radius: 2px;
    box-shadow: 0 2px 8px rgba(0, 0, 0, .33);
    transition: all .3s ease;
    font-family: Helvetica, Arial, sans-serif;
    position: relative;
}

.modal-header h3 {
    margin-top: 0;
    color: #42b983;
}

.modal-default-button {
    float: right;
}

.modal-enter {
    opacity: 0;
}

.modal-leave-active {
    opacity: 0;
}

.modal-enter .modal-container,
.modal-leave-active .modal-container {
    -webkit-transform: scale(1.1);
    transform: scale(1.1);
}
</style>
